package com.jm.product.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jm.product.controller.core.BaseController;
import com.jm.product.entity.SpecValues;
import com.jm.product.entity.Specification;
import com.jm.product.service.SpecValuesService;

/**
 * 商品规格Controller
 * @author sunshine
 * @version 2017-07-03
 */
@Controller
@RequestMapping(value = "product/specification")
public class SpecificationController extends BaseController<Specification,Integer> {

	@Autowired
	private SpecValuesService specValuesService ;
	protected String listPage() {

		return "product/specificationList";
	}

	public String form(Specification specification, Model model) {
		if (null != specification && null != specification.getId()){
			this.data = service.getById(specification.getId());
			model.addAttribute("specification", this.data);
		}
		return "product/specificationForm";
	}

	public String savePage() {

		return "redirect:/product/specification/?repage";
	}

    @RequestMapping(value = "save")
    public String save(Specification specification, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) {
		if (!beanValidator(model, specification)){
			return form(specification, model);
		}
		List<SpecValues> specValuesList = specification.getSpecValuesList() ;
        if(specification != null && specValuesList != null && !specValuesList.isEmpty()){
			for(SpecValues specValues : specValuesList){
				specValues.setSpecification(specification);
			}
        }
        return super.save(specification, model, redirectAttributes, request);
	}

    @RequestMapping(value = "deleteChild")
    @ResponseBody
    public Map<String, Object> deleteChild(@RequestParam Integer id, RedirectAttributes redirectAttributes) {
		Map<String, Object> returnMap = Maps.newHashMap() ;
		try {
			this.specValuesService.removeById(id);
			returnMap.put(SUCCESS, true);
			returnMap.put(MESSAGE, "删除成功") ;
		} catch (Exception e) {
			returnMap.put(SUCCESS, false);
			returnMap.put(MESSAGE, "删除失败") ;
			e.printStackTrace();
		}
		return returnMap;
    }

    @RequestMapping("formTable")
    public String formTable(Specification specification, HttpServletRequest request, HttpServletResponse response, Model model){
		if (null != specification && null != specification.getId()){
			this.data = service.getById(specification.getId());
			model.addAttribute("specification", this.data);
		}
    	return "product/specificationFormTable";
    }

    @RequestMapping("getTableData")
    @ResponseBody
    public List<SpecValues> getSpecValuesList(Specification specification, HttpServletRequest request, HttpServletResponse response, Model model){
		if (null != specification && null != specification.getId()){
			this.data = service.getById(specification.getId());
			if(this.data != null)
				return this.data.getSpecValuesList() ;
		}
        return Lists.newArrayList();
	}

    @RequestMapping("viewFormTable")
    public String viewFormTable(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response, Model model){
		Specification specification = this.service.getById(id) ;
    	model.addAttribute("specification",specification) ;
    	return "product/specificationViewFormTable";
    }

}
